package com.rtx.wms.customer.grp.handler.fs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.edi.platform.synctask.api.ExternLogic;
import com.rtx.edi.platform.synctask.bean.bo.ExternLogicBo;
import com.rtx.wms.std.asn.service.ReceiptService;
import com.rtx.wms.std.so.bean.Orders;
import com.rtx.wms.std.so.service.OrdersService;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpRequest;


/**
 * 出入库单反审
 * @author Administrator
 *
 */
@SuppressWarnings("all")
@Component("SoAndAsnFSHandle-response")
public class SoAndAsnFSHandle implements ExternLogic{
	
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    OrdersService ordersService;
    
    @Autowired
    ReceiptService receiptService;
    
    @Value("${riswms.so.fs}")
    private String so_fs_url;
    
    @Value("${riswms.asn.fs}")
    private String asn_fs_url;


    
    public void handle(ExternLogicBo bo){
    	String request = bo.getRequest();
    	String response = bo.getResponse();

    	String type = JSON.parseObject(request).getString("flag");
    	String numkey = JSON.parseObject(request).getString("numkey");
    	String code = JSON.parseObject(response).getString("code");
    	String whseid = JSON.parseObject(request).getString("whseid");

    	String ediflag ="";

    	//ERP返回成才会继续处理
    	if(!"0".equals(code)) {
    		ediflag ="PF";
    	}else {
    		ediflag ="PS";
    	}

    	if("FH".equals(type)) {
    		LambdaUpdateWrapper<Orders> upd = Wrappers.lambdaUpdate();
    		upd.eq(Orders::getOrderkey, numkey);
    		upd.set(Orders::getRtxeditflag_erp_ps, ediflag);

    		ordersService.updateByWrapper(whseid, upd);

    		if("PS".equals(type)) {
    			JSONObject sorequest = new JSONObject();
    			sorequest.put("whseid", whseid);
    			sorequest.put("editwho", "edi");
    			sorequest.put("orderkey", numkey);

    			callWmsAPI(sorequest.toJSONString(),"调用riswms出库单反审");
    		}
    	}else if("SH".equals(type)) {
    		LambdaUpdateWrapper<Orders> upd = Wrappers.lambdaUpdate();
    		upd.eq(Orders::getOrderkey, numkey);
    		upd.set(Orders::getRtxeditflag_erp_ps, ediflag);

    		ordersService.updateByWrapper(whseid, upd);

    		if("PS".equals(type)) {
    			JSONObject asnrequest = new JSONObject();
    			asnrequest.put("whseid", whseid);
    			asnrequest.put("editwho", "edi");
    			asnrequest.put("receiptkey", numkey);

    			callWmsAPI(asnrequest.toJSONString(),"调用riswms入库单反审");
    		}
    	}else {
    		throw new RuntimeException("出入库反审核flag不为FH、SH其中一个!");
    	}

    }
    
    
    private void callWmsAPI(String request,String apiname) {
    	String uid = UUID.randomUUID().toString();
    	logger.info("uid-{}-{}-请求-{}",uid,apiname,request);
    	String result = HttpRequest
    					.post(so_fs_url)
    					.body(request)
    					.timeout(30000)
    					.execute()
    					.body();
    	logger.info("uid-{}-{}-响应-{}",uid,apiname,result);
    	JSONObject response = JSON.parseObject(result);
    	String code = response.getString("200");
    	if(!"200".equals(code)) {
    		throw new RuntimeException(result);
    	}
    }
    
}
